package com.leetcode.day8_1;

import java.util.ArrayList;
import java.util.List;

/**
 * author: the_ring
 * Time: 2021/8/1 20:04
 * log: 矩阵中战斗力最弱的 K 行
 * link: https://leetcode-cn.com/problems/the-k-weakest-rows-in-a-matrix/
 */
public class Day3Code1 {
    public static void main(String[] args) {
        int[][] mat = new int[][]{
                {1, 1, 0, 0, 0},
                {1, 1, 1, 1, 0},
                {1, 0, 0, 0, 0},
                {1, 1, 0, 0, 0},
                {1, 1, 1, 1, 1},
        };
        int[] result = new Solution().kWeakestRows(mat, 3);
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}

class Solution {
    public int[] kWeakestRows(int[][] mat, int k) {
        int[] result = new int[k];
        int[] temp = new int[mat.length];
        for (int i = 0; i < mat.length; i++) {
            int j = 0;
            for (; j < mat[i].length && mat[i][j] == 1; j++)
            temp[i] = j + 1;
        }
        int minIndex = 0;
        for (int i = 0; i < k; i++) {
            int min = Integer.MAX_VALUE;
            int index = 0;
            for (; index < temp.length; index++) {
                if (min > temp[index]) {
                    min = temp[index];
                    minIndex = index;
                }
            }
            temp[minIndex] = Integer.MAX_VALUE;
            result[i] = minIndex;
        }
        return result;
    }
}